/*
 * CanaryApp.java
 */

package canary;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class CanaryApp extends SingleFrameApplication {
    private CanaryView mainWindow;
    private JFrame frame;
    private boolean verbose = false;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        mainWindow = new CanaryView(this);
        frame = mainWindow.getFrame();
        frame.addWindowListener(new MainFrameListener());
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        show(mainWindow);
    }

     @Override
     protected void shutdown() {
         if (frame != null) {
            frame.setVisible(false);
         }
     }

    @Override
    protected void initialize(String[] args) {
        for (String arg : args) {
            if (("--verbose".equals(arg)) || ("-v".equals(arg))) {
                verbose = true;
            } else if (("--help".equals(arg)) || ("-h".equals(arg))) {
                showUsage();
                exit();
            } else {
                System.err.println("Error: invalid option for Canary_II");
                showUsage();
                exit();
            }
        }
    }

    private void showUsage() {
        System.out.println();
        System.out.println("Usage: Canary_II [options]");
        System.out.println();
        System.out.println("Options:");
        System.out.println("\t--verbose, -v\tPrint messages of interest on stdout");
        System.out.println();
    }

    protected boolean beVerbose() {
        return verbose;
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

     private class MainFrameListener extends WindowAdapter {
        @Override
         public void windowClosing(WindowEvent e) {
            exit();
         }
     }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of CanaryApp
     */
    public static CanaryApp getApplication() {
        return Application.getInstance(CanaryApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(CanaryApp.class, args);
    }
}
